﻿using System.Linq;
using System.Xml.Linq;
using SharePhone.Common;

namespace SharePhone.Provider.WebServices
{
    public delegate void GetUserProfileByNameCompletedHandler(object sender, RequestCompleteEventArgs e);

    public delegate void ModifyUserPropertyByAccountNameHandler(object sender, RequestCompleteEventArgs e);

    public class SPUserProfileService : BaseWebService
    {
        private const string URL = "/_vti_bin/UserProfileService.asmx";

        private const string ActionGetUserProfileByName =
            "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/GetUserProfileByName";

        private const string ActionModifyUserPropertyByAccountName =
            "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService/ModifyUserPropertyByAccountName";

        private const string SoapGetUserProfileByName =
            @"<GetUserProfileByName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'><AccountName>{0}</AccountName></GetUserProfileByName>";

        private const string SoapModifyUserPropertyByAccountName =
            @"<ModifyUserPropertyByAccountName xmlns='http://microsoft.com/webservices/SharePointPortalServer/UserProfileService'><accountName>{0}</accountName><newData>{1}</newData></ModifyUserPropertyByAccountName>";

        private readonly CredentialSettings credentials;

        public SPUserProfileService(string webUrl, CredentialSettings credentials)
        {
            BaseUrl = webUrl;
            this.credentials = credentials;
        }

        public event GetUserProfileByNameCompletedHandler GetUserProfileByNameCompleted;
        public event ModifyUserPropertyByAccountNameHandler ModifyUserPropertyByAccountNameCompleted;

        public void GetUserProfileByNameAsync(string name)
        {
            GetResponse(ActionGetUserProfileByName, string.Format(SoapGetUserProfileByName, name),
                        GetNetworkCredential(credentials), (object sender, RequestCompleteEventArgs e) =>
                                                               {
                                                                   XElement result =
                                                                       e.Result.Descendants(
                                                                           XName.Get("GetUserProfileByNameResult",
                                                                                     "http://microsoft.com/webservices/SharePointPortalServer/UserProfileService"))
                                                                           .FirstOrDefault();
                                                                   OnGetUserProfileByNameCompleted(result);
                                                               });
        }

        public void ModifyUserPropertyByAccountNameAsync(string accountName, string propertiesXml)
        {
            GetResponse(ActionModifyUserPropertyByAccountName,
                        string.Format(SoapModifyUserPropertyByAccountName, accountName, propertiesXml),
                        GetNetworkCredential(credentials),
                        (object sender, RequestCompleteEventArgs e) => { XElement result = e.Result; });
        }

        internal override string GetUrl()
        {
            return BaseUrl + URL;
        }

        protected void OnGetUserProfileByNameCompleted(XElement result)
        {
            if (GetUserProfileByNameCompleted != null)
                GetUserProfileByNameCompleted(this, new RequestCompleteEventArgs(result));
        }

        protected void OnModifyUserPropertyByAccountNameCompleted(XElement result)
        {
            if (ModifyUserPropertyByAccountNameCompleted != null)
                ModifyUserPropertyByAccountNameCompleted(this, new RequestCompleteEventArgs(result));
        }
    }
}